package com.itheima;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentMangerTest {
	public static void main(String[] args) throws IOException {
//		ArrayList<Student> array = new ArrayList<Student>();
		String fileName = "student.txt";
		while(true) {
			
			System.out.println("---欢迎来到学生管理系统---");
			System.out.println("1 查询所有学生");
			System.out.println("2 添加学生");
			System.out.println("3 删除学生");
			System.out.println("4 修改学生");
			System.out.println("5 退出");
			
			Scanner sc = new Scanner(System.in);
			String choiceString = sc.nextLine();
			
			switch(choiceString) {
			case "1":
				//查询
				findAllStudent(fileName);
				break;
			case "2":
				//添加
				addStudent(fileName);
				break;
			case "3":
				//删除
				deletStudent(fileName);
				break;
			case "4":
				//修改
				updateStudent(fileName);
				break;
			case "5":
				//退出
				//break;
			default:
				System.out.println("谢谢使用！");
				System.exit(0);//JVM退出
				break;
			}
		}
	}
	
	//
	public static void readData (String fileName,ArrayList<Student> array) throws IOException{
		//
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		String line;
		while((line=br.readLine()) != null) {
			String[] datas = line.split(",");
			Student s = new Student();
			s.setId(datas[0]);
			s.setName(datas[1]);
			s.setAge(datas[2]);
			s.setAddress(datas[3]);
			array.add(s);
		}
		br.close();
	}
	
	//
	public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
		for(int i=0;i<array.size();i++) {
			Student s = new Student();
			s = array.get(i);
			StringBuilder sb = new StringBuilder();
			sb.append(s.getId()+","+s.getName()+","+s.getAge()+","+s.getAddress());
			bw.write(sb.toString());
			bw.newLine();
			bw.flush();
		}
		bw.close();
	}
	
	//
	public static void findAllStudent(String fileName) throws IOException {
		ArrayList<Student> array = new ArrayList<Student>();
		readData(fileName, array);
		if(array.size() == 0) {
			System.out.println("暂时没有可以查询的学生，请重新选择！");
		}else {
			System.out.println("学号\t\t姓名\t年龄\t居住地");
			for(int i=0;i<array.size();i++) {
				Student s = array.get(i);
				System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
			}
		}
	}
	//
	public static void addStudent(String fileName) throws IOException {
		ArrayList<Student> array = new ArrayList<Student>();
		readData(fileName, array);
		Scanner sc = new Scanner(System.in);
		String id;
		while(true) {
			System.out.println("请输入学号：");
			id = sc.nextLine();
			boolean flag = false;
			for(int i=0;i<array.size();i++) {
				Student s = new Student();
				s = array.get(i);
				if(s.getId().equals(id)) {
					flag = true;
					break;
				}
			}
			if(flag) {
				System.out.println("该学号已经使用，请重新输入：");
			}else {
				break;
			}
		}
		Student s = new Student();
		s.setId(id);
		System.out.println("请输入学生姓名：");
		String name = sc.nextLine();
		System.out.println("请输入学生年龄：");
		String age = sc.nextLine();
		System.out.println("请输入学生居住地：");
		String address = sc.nextLine();
		s.setName(name);
		s.setAge(age);
		s.setAddress(address);
		array.add(s);
		writeData(fileName, array);
		System.out.println("添加成功!");
	}
	//删除
	public static void deletStudent(String fileName) throws IOException {
		ArrayList<Student> array = new ArrayList<Student>();
		readData(fileName, array);
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要删除学生学号：");
		String id = sc.nextLine();
		int index = -1;
		for(int i=0;i<array.size();i++) {
			Student s = new Student();
			s = array.get(i);
			if(s.getId().equals(id)) {
				index = i;
				break;
			}
		}
		if(index == -1) {
			System.out.println("该学号不存在，请重新选择！");
		}else {
			array.remove(index);
			writeData(fileName, array);
			System.out.println("删除成功！");
		}
		
	}
	//修改
	public static void updateStudent(String fileName) throws IOException {
		ArrayList<Student> array = new ArrayList<Student>();
		readData(fileName, array);
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要修改学生的学号：");
		String id = sc.nextLine();
		int index = -1;
		for(int i=0;i<array.size();i++) {
			Student s = new Student();
			s = array.get(i);
			if(s.getId().equals(id)) {
				index = i;
				break;
			}
		}
		if(index == -1) {
			System.out.println("你要修改学生不存在，请重新选择！");
		}else {
			System.out.println("请输入学生姓名：");
			String name = sc.nextLine();
			System.out.println("请输入学生年龄：");
			String age = sc.nextLine();
			System.out.println("请输入学生地址：");
			String address = sc.nextLine();
			
			Student s = new Student(id,name,age,address);
			array.set(index, s);
			writeData(fileName, array);
			System.out.println("修改成功！");
		}
	}
}




